import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

export default new Vuex.Store({
	modules: {
		duty: {
			namespaced: true,
			state: {
				scheduleData: {}, // 所有排班数据
				selectedDates: [], // 已排班日期
				currentMonth: '', // 当前缓存月份
			},
			mutations: {
				SET_SCHEDULE_DATA(state, data) {
					state.scheduleData = data;
					uni.setStorageSync('scheduleData', data);
				},
				SET_SELECTED_DATES(state, dates) {
					state.selectedDates = dates;
					uni.setStorageSync('selectedDates', dates);
				},
				SET_CURRENT_MONTH(state, month) {
					state.currentMonth = month;
					uni.setStorageSync('currentMonth', month);
				},
			},
			actions: {
				saveScheduleData({
					commit
				}, {
					month,
					data,
					selectedDates
				}) {
					commit('SET_SCHEDULE_DATA', data);
					commit('SET_SELECTED_DATES', selectedDates);
					commit('SET_CURRENT_MONTH', month);
				},
				loadFromLocalStorage({
					commit
				}) {
					const scheduleData = uni.getStorageSync('scheduleData') || {};
					const selectedDates = uni.getStorageSync('selectedDates') || [];
					const currentMonth = uni.getStorageSync('currentMonth') || '';
					commit('SET_SCHEDULE_DATA', scheduleData);
					commit('SET_SELECTED_DATES', selectedDates);
					commit('SET_CURRENT_MONTH', currentMonth);
				}
			}
		}
	}
});